$(function () {
    /*邀请码 验证码验证 end*/
    if ($("#regfrom").length) {
        $("#regfrom").validate({
            rules: {
                upwd: {
                    required: true,
                    minlength:6,
                    maxlength: 20
                },
                cfmpwd: {
                    required: true,
                    equalTo: "#upwd"
                },phone: {
                    required: true,
                }
            }, messages: {
                upwd: {
                    required: "请输入确认密码",
                    minlength: "密码最少为6位",
                    maxlength: "密码最多为20个字符",
                    equalTo: "两次输入密码不一致"
                },
                cfmpwd: {
                    required: "请输入密码"
                },
                phone: {
                    required: "请输入手机号"
                }
            },
            errorClass: "warnlabel",
            focusCleanup: true
        });
    }


    if ($("#login").length) {
        $("#login").validate({
            rules: {
                uname: {
                    required: true
                },
                upwd: {
                    required: true,
                    minlength: 3,
                    maxlength: 20
                }
            }, messages: {
                uname: {
                    required: "用户名必须填写"
                },
                upwd: {
                    required: "密码不能为空",
                    minlength: "密码最少为6位",
                    maxlength: "密码最多为20个字符"
                }
            },
            errorClass: "warnlabel",
            focusCleanup: true,
            submitHandler: function () {
                $(".gologin").val("登陆中...");
                $("#resultError").empty();
                var data = {};
                data["uname"] = $("#uname").val(), data["upwd"] = $("#upwd").val();

                $.post("/home/loginDo", data, function (e) {
                    $(".gologin").val("登陆");
                    if (!(e.Msg === "Ok")) {
                        $("#resultError").html(e.Msg);
                        return false;
                    }
                    //返回页面
                    window.location = getReturnUrl();

                });
            }

        })
    }



    var getReturnUrl = function () {
        var strHref = this.location.href;
        var returnurl = strHref.getQuery("returnurl");

        if (returnurl == null || returnurl == "") {
            returnurl = baseuri + "ucenter/index.aspx";
        }
        return returnurl;
    }
    var warnMsg = function (e, m) {
        var e = $(e);
        if (e.next().is(".warnlabel")) {
            e.next(".warnlabel").text(m);
            return false;
        }
        var warnlabel = "<label class='warnlabel'>" + m + "</label>"
        e.addClass('warnborder').after(warnlabel);
    }
    var verifyed = function (e) {
        if (e.next().is(".warnlabel")) {
            e.removeClass('warnborder').next(".warnlabel").remove();
        }
    }
    // 注册end
    // 订单列表
    $(".order-filter A").click(function () {
        $(this).addClass("order-filter-cur").siblings("A").removeClass("order-filter-cur");
    });
    // 订单列表end

    // 充值
    $(".gotocharge").click(function () {
        var n = $("#charge-money").val();
        if (n) {
            if (isNaN(parseInt(n))) {
                warnMsg("#charge-money", "请输入正确的充值金额");
            }
            else {
                verifyed($("#charge-money"));
            }
        }
        else {
            warnMsg("#charge-money", "请输入充值金额");
        }
    });
    $(".gochagecard").click(function () {
        if (!$("#card-no").val()) {
            warnMsg("#card-no", "请输入购物卡卡号");
        }
        if (!$("#card-pwd").val()) {
            warnMsg("#card-pwd", "请输入购物卡密码");

        }
        if (!$("#card-code").val()) {
            warnMsg("#card-code", "请输入应用码");

        }
        if (!$("#your-acc").val()) {
            warnMsg("#your-acc", "请输入需要充值的帐号");
        }
        var t = true;
        $(".shop-card INPUT").each(function (index, element) {
          
            if (!$(this).val()) {
                t = false;
                return false;
            }
           
        });
        if (t) {
            var data = { sCardNo: $("#card-no").val(), sCardCode: $("#card-code").val(), sCardPwd: $("#card-pwd").val() };
            $.post(baseuri + "ucenter/chargecard.aspx?&act=bindcard", data, function (result) {
                if (result.Msg === 'Ok') {
                    Message("充值成功!");
                    window.setTimeout(function () { location.reload(); }, 1000);
                } else {
                    Message(result.Msg)
                }
            });
        }
    });
    $(".shop-card INPUT").click(function () {
        verifyed($(this));
    });
    // 充值end

    // 修改密码
    $("#oldupwd").blur(
    function () {
        var pwd = $(this).val();
        if (pwd.length > 0) {
            if (pwd.length >= 1 && pwd.length <= 20) {
                verifyed($(this));
            }
            else {
                warnMsg($(this), "密码长度只能在6-20位字符之间");
            }
        }
    }
    );
    $("#upwd").blur(
function () {
    var pwd = $(this).val();
    if (pwd.length > 0) {
        if (pwd.length >= 6 && pwd.length <= 20) {
            verifyed($(this));
        }
        else {
            warnMsg($(this), "密码长度只能在6-20位字符之间");
        }
    }
}
);
    $(".editpwd").click(function () {
        if (!$("#oldupwd").val()) {
            warnMsg("#oldupwd", "请输入密码");
        }
        if (!$("#upwd").val()) {
            warnMsg("#upwd", "请输入密码");
        }
        if (!$("#cfmpwd").val()) {
            warnMsg("#cfmpwd", "请输入确认密码");

        }
        if ($("#cfmpwd").val() != $("#upwd").val()) {
            $("#cfmpwd").val("");
            warnMsg("#cfmpwd", "两次密码不正确,请重新设置");

        }
        var t = true;
        $(".reg-infos INPUT").not("#invitecode,#agreement").each(function (index, element) {
          
            if (!$(this).val()) {
                t = false;
                return false;
            }
          
        });
        if (t) {
            var data = { oldupwd: $("#oldupwd").val(), upwd: $("#upwd").val() };
            $.post("/customer/updatePassword", data, function (result) {
                if (result.success) {
                    Message(result.msg);
                    window.setTimeout(function () { location.href = "/customer";}, 1000);
                } else {
                    Message(result.msg);
                }
            },"json");
        }
    });
    // 修改密码end

});